package com.bugsnag.ohos;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilityLifecycleCallbacks;
import ohos.utils.PacMap;

import com.bugsnag.ohos.util.OhosLog;

import java.util.HashMap;
import java.util.Map;

/**
 * AbilityBreadcrumbCollector
 *
 * @since 2021-07-02
 */
public class AbilityBreadcrumbCollector implements AbilityLifecycleCallbacks {
    private static final String TAG = "AbilityBreadcrumbCollector";
    String prevState = null;
    private BreadcrumbCollectorCallback collectorCallback;

    /**
     * BreadcrumbCollectorCallback
     *
     * @since 2021-07-26
     */
    public interface BreadcrumbCollectorCallback {
        /**
         * call back
         *
         * @param message
         * @param method
         */
        void callback(String message, Map<String, Object> method);
    }

    /**
     * constructor
     *
     * @param collectorCallback
     */
    public AbilityBreadcrumbCollector(BreadcrumbCollectorCallback collectorCallback) {
        this.collectorCallback = collectorCallback;
    }

    @Override
    public void onAbilityStart(Ability ability) {
        OhosLog.i(TAG, "onAbilityStart %s", ability);
        leaveBreadcrumb(getActivityName(ability), "onStart()");
    }

    @Override
    public void onAbilityActive(Ability ability) {
        OhosLog.i(TAG, "onAbilityActive %s", ability);
        leaveBreadcrumb(getActivityName(ability), "onActive()");
    }

    @Override
    public void onAbilityInactive(Ability ability) {
        OhosLog.i(TAG, "onAbilityInactive %s", ability);
        leaveBreadcrumb(getActivityName(ability), "onInactive()");
    }

    @Override
    public void onAbilityForeground(Ability ability) {
        OhosLog.i(TAG, "onAbilityForeground %s", ability);
        leaveBreadcrumb(getActivityName(ability), "onForeground()");
    }

    @Override
    public void onAbilityBackground(Ability ability) {
        OhosLog.i(TAG, "onAbilityBackground %s", ability);
        leaveBreadcrumb(getActivityName(ability), "onBackground()");
    }

    @Override
    public void onAbilityStop(Ability ability) {
        OhosLog.i(TAG, "onAbilityStop %s", ability);
        leaveBreadcrumb(getActivityName(ability), "onStop()");
    }

    @Override
    public void onAbilitySaveState(PacMap pacMap) {
        leaveBreadcrumb("Ability", "onAbilitySaveState()");
    }

    private String getActivityName(Ability ability) {
        return ability.getClass().getName();
    }

    private void leaveBreadcrumb(String abilityName, String lifecycleCallback) {
        leaveBreadcrumb(abilityName, lifecycleCallback, false);
    }

    private void leaveBreadcrumb(String abilityName, String lifecycleCallback, boolean hasBundle) {
        Map metadata = new HashMap<String, Object>();
        String previousVal = prevState;
        if (previousVal != null) {
            metadata.put("previous", previousVal);
        }
        if ((Boolean) hasBundle != null) {
            metadata.put("hasBundle", hasBundle);
        }
        collectorCallback.callback(abilityName + "#" + lifecycleCallback, metadata);
        prevState = lifecycleCallback;
    }
}
